package com.woniu.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.TaskDto;
import com.woniu.entity.Task;
import com.woniu.service.TaskService;
import com.woniu.util.PageData;
import com.woniu.util.ResponseData;
import com.woniu.vo.AlarmMessageVo;
import com.woniu.vo.TaskVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/task")
@Slf4j
@CrossOrigin(origins = "*")
public class TaskController {
    @Autowired
    private TaskService taskService;


    /*
    * 新增
    * */
    @RequestMapping("/addTask")
    public ResponseData<AlarmMessageVo> addTask(@RequestBody TaskDto task){
        log.info("task----->, {}", task);
        taskService.addTask(task);
        return ResponseData.ok();
    }

    /*
    * 修改
    * */
    @RequestMapping("/updateTask")
    public ResponseData updateTask(@RequestBody Map<String, Object> taskMap){
        for (Object o : taskMap.keySet()) {
            log.info(o+"-----------> {}", taskMap.get(o));
        }

        taskService.updateTask(taskMap);

       /* taskService.updateTask(task);*/
        return ResponseData.ok();
    }

    /*
    * 分页查询任务
    * */
    @RequestMapping("pageTask")
    public ResponseData<PageData> pageTask(@RequestBody PageData pageData){
        Page page = taskService.pageTask(pageData);

        return ResponseData.ok(page);
    }


    /*
    * 单查
    * */
    @GetMapping("queryById")
    public ResponseData<TaskVo> queryById(@RequestParam("taskNumber") String taskNumber){
        System.out.println(taskNumber);
        TaskVo task = taskService.queryById(taskNumber);
        return ResponseData.ok(task);
    }

   @GetMapping("taskExcel")
    public ResponseData<?> toTaskExcel(HttpServletResponse response) throws IOException {
        return taskService.toTaskExcel(response);
   }
}
